I'm very new to DOORS DXL, so I apologize in advance. I'm trying to write a very simple script to find a phrase of rich text with possible formatting and replace it with another phrase of rich text. The problem is that if the phrase I'm looking for contains any rich text formatting I cannot find it, in my instance a bolded NOT. Any help and tips are most appreciated and I've attached my script below(mostly stolen from another thread). Thanks! |
Re: Finding a rich text phrase and replacing it with different rich text I think you did fine - but if I understand this correctly - you have comment about an occurence of "NOT" in your "_Expected Results" attribute but your script is looking for "not be inhibited". Your script works fine for the current object containing a bold "not be inhibited", as you have the mod loop commented out. Sara |
Re: Finding a rich text phrase and replacing it with different rich text smarti.sj - Fri Jan 06 12:20:10 EST 2012 Unfortunately, I probably should have explained my comment about "NOT" a bit more. My phrases of "NOT be inhibited" will replace if the NOT is not bolded, if the NOT in the phrase is bolded they won't replace. When I print the rich text string of my phrase I get "\b NOT \b0 be inhibited". I've tried some roundabout attempts, to not success. |
Re: Finding a rich text phrase and replacing it with different rich text
The following is not quite a complete solution. Maybe one of the rich-text experienced folks will jump in here with something far more elegant or at least clean up my mess.
//*****************
string fExtractRT(string TextRich, int StartLoc, EndLoc, LenRaw)
{ // Extract the portion of the Rich Text string in the TextRich
// as identified by the starting and ending locations.
// Preserve the TextRich's RichText markup
// StartLoc and EndLoc and LenRaw MUST be relative to the equivalent RAW text,
// perhaps by calling Loc = fGetOffset(TextRaw, SearchString).
// Strategy: only mechanism for extracting out of a rich text chunk is using cutRichText().
// Thus, we cut the part after EndLoc, then cut the part before StartLoc, leaving what we want.
string OutText
if (EndLoc < StartLoc or
EndLoc > LenRaw or
StartLoc < 0) return("") // Program Input Error
if (EndLoc == LenRaw)
OutText = TextRich
else OutText = cutRichText(TextRich, EndLoc+1, LenRaw) // Strip Ending
// print ":::: Extract, TextRich, OutText = >>" TextRich "<<< >>>" OutText "<<\n"
if (StartLoc > 0)
OutText = cutRichText(OutText, 0, StartLoc-1) // Strip Beginning
// print ">>>>>> Extract: In/Start/End/Tot/Out = >>>" TextRich "<<< " StartLoc " " //-
// EndLoc " " LenTotal " >>>" OutText "<<<\n"
return(OutText)
} // end fExtractRT()
//-------------------
string fExtractRT(string TextRich, int StartLoc, EndLoc)
{ // Overloaded, doesn't need input Raw Text Length
int LenText = length(TextRich) // Use rich text length, doesn't matter.
return(fExtractRT(TextRich, StartLoc, EndLoc, LenText))
} // end fExtractRT()
//*********************
string fStripRT(string InString)
{ // Legacy
return(plainText(InString))
} // end fStripRT(string)
//*********************
void fStripRT(Buffer &in_buf)
{ // Legacy
// in_buf = plainText(stringOf(in_buf))
in_buf = plainText(tempStringOf(in_buf))
} // end fStripRT(Buffer)
//****************************************************
string fReplaceRawText(string InRich, StringFrom, StringTo, bool MatchCase)
{ // Replace all occurrences of the raw StringFrom to the raw StringTo in the
// Rich Text string InRich. RichText markup in InRich is ignored but preserved.
// Note: Even for simple RichText strings, this function returns a very large string
// featuring lots of RTF header info. User may want to issue "removeUnlistedRichText"
// after calling this function.
// Developers Note: This is a little tricky since some StringFrom may be part of the Markup,
// but needs to be ignored.
// Strategy: Get the Raw representation of the string. Search for the StringFrom.
// if found, build a new RichText string composed of the part before the found location,
// the StringTo, and the part after the found location. That last part must be
// recursively searched before its appended.
if (null InRich or null StringFrom) return("")
string InRaw, FirstPart, LastPart
int LenFrom, LocFrom, LenRaw
InRaw = fStripRT(InRich)
LocFrom = fGetOffset(InRaw, StringFrom, MatchCase)
if (LocFrom < 0) return(InRich) // StringFrom not found, nothing to do
LenRaw = length(InRaw)
LenFrom = length(StringFrom)
if (LocFrom == 0)
FirstPart = "" // Found in the first position.
else FirstPart = fExtractRT(InRich, 0, LocFrom-1, LenRaw)
if (LocFrom+LenFrom-1 >= LenRaw)
LastPart = "" // Found as last position(s) in the string
else
{ LastPart = fExtractRT(InRich, LocFrom+LenFrom, LenRaw-1, LenRaw)
LastPart = fReplaceRawText(LastPart, StringFrom, StringTo, MatchCase) // ** RECURSIVE **
}
// print "**LenRich/LenRaw =\t" (length(InRich)) "\t" LenRaw "\n"
// print "**LocFrom =\tSearching for \t<">, found at \t" LocFrom "\n"
// print "**FirstPart =\t" FirstPart "\n"
// print "**LastPart =\t" LastPart "\n"
return(fConCat(FirstPart, StringTo, LastPart))
} // end fReplaceRawText()
|
Re: Finding a rich text phrase and replacing it with different rich text llandale - Mon Jan 09 17:30:08 EST 2012
The following is not quite a complete solution. Maybe one of the rich-text experienced folks will jump in here with something far more elegant or at least clean up my mess.
//*****************
string fExtractRT(string TextRich, int StartLoc, EndLoc, LenRaw)
{ // Extract the portion of the Rich Text string in the TextRich
// as identified by the starting and ending locations.
// Preserve the TextRich's RichText markup
// StartLoc and EndLoc and LenRaw MUST be relative to the equivalent RAW text,
// perhaps by calling Loc = fGetOffset(TextRaw, SearchString).
// Strategy: only mechanism for extracting out of a rich text chunk is using cutRichText().
// Thus, we cut the part after EndLoc, then cut the part before StartLoc, leaving what we want.
string OutText
if (EndLoc < StartLoc or
EndLoc > LenRaw or
StartLoc < 0) return("") // Program Input Error
if (EndLoc == LenRaw)
OutText = TextRich
else OutText = cutRichText(TextRich, EndLoc+1, LenRaw) // Strip Ending
// print ":::: Extract, TextRich, OutText = >>" TextRich "<<< >>>" OutText "<<\n"
if (StartLoc > 0)
OutText = cutRichText(OutText, 0, StartLoc-1) // Strip Beginning
// print ">>>>>> Extract: In/Start/End/Tot/Out = >>>" TextRich "<<< " StartLoc " " //-
// EndLoc " " LenTotal " >>>" OutText "<<<\n"
return(OutText)
} // end fExtractRT()
//-------------------
string fExtractRT(string TextRich, int StartLoc, EndLoc)
{ // Overloaded, doesn't need input Raw Text Length
int LenText = length(TextRich) // Use rich text length, doesn't matter.
return(fExtractRT(TextRich, StartLoc, EndLoc, LenText))
} // end fExtractRT()
//*********************
string fStripRT(string InString)
{ // Legacy
return(plainText(InString))
} // end fStripRT(string)
//*********************
void fStripRT(Buffer &in_buf)
{ // Legacy
// in_buf = plainText(stringOf(in_buf))
in_buf = plainText(tempStringOf(in_buf))
} // end fStripRT(Buffer)
//****************************************************
string fReplaceRawText(string InRich, StringFrom, StringTo, bool MatchCase)
{ // Replace all occurrences of the raw StringFrom to the raw StringTo in the
// Rich Text string InRich. RichText markup in InRich is ignored but preserved.
// Note: Even for simple RichText strings, this function returns a very large string
// featuring lots of RTF header info. User may want to issue "removeUnlistedRichText"
// after calling this function.
// Developers Note: This is a little tricky since some StringFrom may be part of the Markup,
// but needs to be ignored.
// Strategy: Get the Raw representation of the string. Search for the StringFrom.
// if found, build a new RichText string composed of the part before the found location,
// the StringTo, and the part after the found location. That last part must be
// recursively searched before its appended.
if (null InRich or null StringFrom) return("")
string InRaw, FirstPart, LastPart
int LenFrom, LocFrom, LenRaw
InRaw = fStripRT(InRich)
LocFrom = fGetOffset(InRaw, StringFrom, MatchCase)
if (LocFrom < 0) return(InRich) // StringFrom not found, nothing to do
LenRaw = length(InRaw)
LenFrom = length(StringFrom)
if (LocFrom == 0)
FirstPart = "" // Found in the first position.
else FirstPart = fExtractRT(InRich, 0, LocFrom-1, LenRaw)
if (LocFrom+LenFrom-1 >= LenRaw)
LastPart = "" // Found as last position(s) in the string
else
{ LastPart = fExtractRT(InRich, LocFrom+LenFrom, LenRaw-1, LenRaw)
LastPart = fReplaceRawText(LastPart, StringFrom, StringTo, MatchCase) // ** RECURSIVE **
}
// print "**LenRich/LenRaw =\t" (length(InRich)) "\t" LenRaw "\n"
// print "**LocFrom =\tSearching for \t<">, found at \t" LocFrom "\n"
// print "**FirstPart =\t" FirstPart "\n"
// print "**LastPart =\t" LastPart "\n"
return(fConCat(FirstPart, StringTo, LastPart))
} // end fReplaceRawText()
That is similar to the approach I ended up using except mine is not generic or elegant in the slightest. I ended up using plain text and since I knew what my RTF was most of the time I included that in my find string and then manhandled what was left. I may try to implement your approach if I have time. Out of curiosity, how did you get away with never using rich-text again? |
Re: Finding a rich text phrase and replacing it with different rich text NickPernsteiner - Wed Jan 11 12:25:13 EST 2012 -Louie |